package com.byfan.springboottemplate.controller;

import com.byfan.springboottemplate.common.BaseResponse;
import com.byfan.springboottemplate.common.CommonResponse;
import com.byfan.springboottemplate.exception.SpringBootTemplateException;
import com.byfan.springboottemplate.model.FileEntity;
import com.byfan.springboottemplate.model.UserEntity;
import com.byfan.springboottemplate.service.IFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @ClassName: FileController
 * @Description:
 * @Author: byfan
 * @Date: 2023/7/28
 */
@Slf4j
@Api(tags = "文件接口")
@RestController
@RequestMapping("/api/file")
public class FileController {

    @Autowired
    private IFileService fileService;


    @ApiOperation("查询全部文件信息")
    @RequestMapping(value = "/getAll",method = RequestMethod.GET)
    public BaseResponse<List<FileEntity>> getAll() {
        BaseResponse<List<FileEntity>> response = new BaseResponse();
        try {
            List<FileEntity> all = fileService.getAll();
            response.setData(all);
            response.setCode(CommonResponse.OK.code);
            return response;
        } catch (SpringBootTemplateException e) {
            log.error("getAll is except ,e: ", e);
            response.setCode(e.getErrorCode());
            response.setMsg(e.getMessage());
            return response;
        }
    }

    @ApiOperation("上传文件")
    @ApiImplicitParam(name = "file", value = "最大10M", required = true)
    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public BaseResponse upload(@RequestBody MultipartFile file) throws SpringBootTemplateException {
        BaseResponse response = new BaseResponse();
        if (file.getSize()/Math.pow(1024,2) > 10){
            log.error("upload file is too large");
            throw new SpringBootTemplateException(CommonResponse.FILE_TOO_LARGE, "文件大小超出限制");
        }
        try {
            FileEntity fileEntity = fileService.upload(file);
            response.setData(fileEntity);
            response.setCode(CommonResponse.OK.code);
            response.setMsg("success");
            return response;
        } catch (SpringBootTemplateException e) {
            log.error("upload except ", e);
            response.setCode(e.getErrorCode());
            response.setMsg(e.getMessage());
            return response;
        }
    }

    @ApiOperation("根据id下载文件")
    @ApiImplicitParam(name = "id", value = "文件id", paramType = "query", required = true)
    @RequestMapping(value = "/download/{id}", method = RequestMethod.GET)
    public void download(@PathVariable Long id, HttpServletResponse response) throws SpringBootTemplateException {
        fileService.download(id, response);
    }

    @ApiOperation("根据路径下载文件")
    @ApiImplicitParam(name = "path", value = "文件路径", paramType = "query", required = true)
    @RequestMapping(value = "/downloadByPath/{path}", method = RequestMethod.GET)
    public void download(@PathVariable String path) throws SpringBootTemplateException {
        fileService.download(path);
    }

    @ApiOperation("根据id删除文件")
    @ApiImplicitParam(name = "id", value = "文件id", paramType = "query", required = true)
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
    public BaseResponse delete(@PathVariable Long id) throws SpringBootTemplateException {
        BaseResponse response = new BaseResponse();
        try {
            fileService.delete(id);
            response.setCode(CommonResponse.OK.code);
            response.setMsg("文件删除成功");
            return response;
        } catch (SpringBootTemplateException e) {
            log.error("delete except ", e);
            response.setCode(e.getErrorCode());
            response.setMsg(e.getMessage());
            return response;
        }
    }

    @ApiOperation("删除文件")
    @ApiImplicitParam(name = "path", value = "文件路径(eg:xxx/xxx.png)", paramType = "query", required = true)
    @RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    public BaseResponse delete(String path) throws SpringBootTemplateException {
        BaseResponse response = new BaseResponse();
        try {
            fileService.delete(path);
            response.setCode(CommonResponse.OK.code);
            response.setMsg("文件删除成功");
            return response;
        } catch (SpringBootTemplateException e) {
            log.error("delete except ", e);
            response.setCode(e.getErrorCode());
            response.setMsg(e.getMessage());
            return response;
        }
    }
}
